
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">{{ page.total }}</span>
        <template slot="after">
          <el-button
            size="small"
            type="warning"
            @click="$router.push('/import')"
          >导入</el-button>
          <el-button
            size="small"
            type="danger"
            @click="exportData"
          >导出</el-button>
          <el-button
            icon="plus"
            type="primary"
            size="small"
            @click="showDialog = true"
          >新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table v-loading="loading" :data="list" border>
          <el-table-column type="index" label="序号" sortable="" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column label="手机号" prop="mobile" sortable="" />
          <el-table-column
            label="聘用形式"
            prop="formOfEmployment"
            :formatter="formatEmployment"
            sortable=""
          />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="头像" align="center">
            <template v-slot="{row}">
              <img
                v-imagerror="require('@/assets/common/klt.jpg')"
                :src="row.staffPhoto "
                style="border-radius: 50%; width: 100px; height: 100px; padding: 10px"
                alt=""
                @click="showQrCode(row.staffPhoto)"
              >
            </template>
          </el-table-column>
          <el-table-column label="入职时间" prop="timeOfEntry" sortable="">
            <template v-slot="obj">{{
              obj.row.timeOfEntry | formatDate
            }}</template>
          </el-table-column>

          <el-table-column label="账户状态" prop="enableState" sortable="">
            <template
              v-slot="{ row }"
            ><el-switch :value="row.enableState === 1" /></template></el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template slot-scope="{ row }">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="editRole(row.id)">角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="delEmployee(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :current-page="page.page"
            :page-size="page.size"
            :total="page.total"
            layout="prev, pager, next"
            @current-change="changePage"
          />
        </el-row>
      </el-card>
    </div>
    <AddDemployee :show-dialog.sync="showDialog" />
    <el-dialog title="二维码" :visible.sync="showCodeDialog">
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
    </el-dialog>

    <!-- 角色 -->
    <AssignRole ref="employee" :show-role-dialog.sync="showRoleDialog" :user-id="userId" />

  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
import AddDemployee from './components/add-employee'
import Qrcode from 'qrcode'
import AssignRole from './components/assign-role'
export default {
  components: {
    AddDemployee,
    AssignRole
  },
  data() {
    return {
      //  接收数组
      list: [],
      page: {
        page: 1,
        size: 10,
        // 显示总数
        total: 0
      },
      // 显示遮罩层
      loading: false,
      showDialog: false,
      showCodeDialog: false, // 二维码
      showRoleDialog: false, // 分配角色的弹层
      userId: null
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      this.loading = true
      console.log(11)
      const { total, rows } = await getEmployeeList(this.page)
      console.log(11)
      console.log(total)
      console.log(rows)
      this.page.total = total
      this.list = rows
      this.loading = false
    },
    // 最新页码
    changePage(newPage) {
      // 赋值最新页码
      this.page.page = newPage
      // 重新获取数据
      this.getEmployeeList()
    },
    // 格式化聘用形式
    formatEmployment(row, column, cellValue, index) {
      const obj = EmployeeEnum.hireType.find((item) => item.id === cellValue)
      return obj ? obj.value : cellValue
    },
    // 删除员工
    async delEmployee(id) {
      try {
        await this.$confirm('确定删除该员工吗')
        // 点击确认id
        await delEmployee(id)
        console.log(id)

        this.$message.success('删除成功')

        // 重新获取数据
        this.getEmployeeList()
      } catch (error) {
        console.log(error)
      }
    },
    exportData() {
      const headers = {
        手机号: 'mobile',
        姓名: 'username',
        入职日期: 'timeOfEntry',
        聘用形式: 'formOfEmployment',
        转正日期: 'correctionTime',
        工号: 'workNumber',
        部门: 'departmentName'
      }
      // 导出excel
      import('@/vendor/Export2Excel').then(async excel => {
        const { rows } = await getEmployeeList({
          page: 1, size: this.page.total
        })
        excel.export_json_to_excel({
          multiHeader: [],
          header: Object.keys(headers),
          data: [['李四', 3000]],
          filename: '员工工资表',
          merges: [],
          autoWidth: true,
          bookType: 'xlsx'
        })
        console.log(Object.keys(headers))
        console.log(rows)
      })
    },
    showQrCode(url) {
      if (url) {
        this.showCodeDialog = true
        // 将地址转为二维码
        this.$nextTick(() => {
          Qrcode.toCanvas(this.$refs.myCanvas, url)
        })
      } else {
        this.$message.warning('该用户没有上传图像')
      }
    },
    async editRole(id) {
      this.userId = id
      await this.$refs.employee.getUserDetailById(id)
      this.showRoleDialog = true
    }
  }
}
</script>

<style>
</style>
